package com.jy.wms.service;

import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.annotions.BaseServiceDescribe;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.common.base.SucessException;
import com.jy.wms.pojo.*;
import com.jy.wms.dao.WmsFailedLibraryMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

/**
 * @ClassName: WmsFailedLibraryService.java
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author LH
 * @Date Tue Jan 23 09:32:56 GMT+08:00 2018
 */

@Service
public class WmsFailedLibraryService extends BaseService<WmsFailedLibraryMapper, WmsFailedLibrary> {
    @Autowired
    private WmsShelvesService wmsShelvesService;

    /**
     * 分页查询  当前仓库，当前货主下的质检状态为   “4”（质检完成） 的预期到货通知单（ASN）
     *
     * @param entity 实体
     * @return Page<WmsFailedLibrary>
     */
    @BaseServiceDescribe(moduleName = "不合格品入库", btnName = "分页查询")
    public Page<WmsEstimatedArrivalOrder> baseSelectPage(WmsEstimatedArrivalOrder entity, String hzid, String ssckid, String ckmc, String hzmc) throws Exception {
        try {
            PageHelper.startPage(entity.getPage(), entity.getRows());
            return (Page<WmsEstimatedArrivalOrder>) dao.baseSelectPage(entity, hzid, ssckid, ckmc, hzmc);
        } catch (Exception e) {
            throw new ServiceException("查询预期到货通知单列表", e);
        }
    }

    /**
     * 根据预期到货明细查询收货表里面质检状态为“不合格”的记录
     *
     * @param asnId 预期到货通知单id
     * @return
     */
    public Page<WmsShelves> getReceiveList(String asnId, Integer page, Integer rows) throws ServiceException {
        try {
            //根据预期到货通知单id查找明细id集合
            //List<String> idList = dao.getIdList(asnId);
            PageHelper.startPage(page, rows);
            return (Page<WmsShelves>) dao.getReceiveList(asnId);
        } catch (Exception e) {
            throw new ServiceException("查询预期到货通知单列表", e);
        }

    }

    /**
     * 库位模糊查询 管控的数据
     *
     * @param show_kwid
     * @return
     */
    public List<WmsLibraryPosition> searchKw(String show_kwid, String ckid, String hzid) throws ServiceException {
        try {

            return dao.searchKw(show_kwid, ckid, hzid);
        } catch (Exception e) {
            throw new ServiceException("库位模糊查询失败", e);
        }
    }

    /**
     * 上架
     *
     * @param list
     * @param ckid
     * @param hzid
     * @throws Exception
     */
    @Transactional
    public void add(List<WmsShelves> list, String ckid, String hzid, HttpSession session) throws Exception {
        //用户信息
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        //sign 是为了区分是正常的上架还是不合格品的上架  （0：不合格品上架，1：正常的上架），两个上架共用同一个方法
        wmsShelvesService.baseInsert(list, ckid, hzid, "0");//上架
        WmsFailedLibrary wmsFailedLibrary = new WmsFailedLibrary();
        Integer num = 0;
        for (WmsShelves ws : list) {
            wmsFailedLibrary.setId(UUIDUtils.uuid());
            wmsFailedLibrary.setAsnId(ws.getYqdhbdid());
            wmsFailedLibrary.setAsnMxId(ws.getYqdhbdmxid());
            wmsFailedLibrary.setHpbh(ws.getShow_bh());
            wmsFailedLibrary.setHpsl(new BigDecimal(ws.getSjsl()));
            wmsFailedLibrary.setShid(ws.getSsshid());//收货id
            wmsFailedLibrary.setKcid(ws.getKcid());
            wmsFailedLibrary.setCkid(ws.getCkid());
            wmsFailedLibrary.setCzr(userinfo.getUser().getName());
            //增加不合格入库
            dao.baseInsert(wmsFailedLibrary);
            num++;
        }
        if (num > 0) {
            throw new SucessException("上架成功");
        } else {
            throw new ServiceException("上架失败");
        }

    }


    /**
     * 通过asn id 查询质检单明细
     *
     * @param asnId
     * @return
     */
    public Page<WmsQualityInspectionListDetailed> getQualityDetail(String asnId, Integer page, Integer rows) throws ServiceException {
        try {
            PageHelper.startPage(page, rows);
            return (Page<WmsQualityInspectionListDetailed>) dao.getQualityDetail(asnId);
        } catch (Exception e) {
            throw new ServiceException("质检单明细查询", e);
        }
    }


    /**
     * 质检明细附件
     *
     * @return
     */
    public Page<WmsQcEnclosure> getQcEnclosure(String asnId, Integer page, Integer rows) throws ServiceException {
        try {
            PageHelper.startPage(page, rows);
            return (Page<WmsQcEnclosure>) dao.getQcEnclosure(asnId);
        } catch (Exception e) {
            throw new ServiceException("质检单附件查询", e);
        }
    }


    /**
     * 插入一条记录
     *
     * @param entity 实体对象
     * @return int
     */
    public Integer baseInsert(WmsFailedLibrary entity) {
        return dao.baseInsert(entity);
    }

    /**
     * 插入一条记录（匹配有值的字段）
     *
     * @param entity 实体对象
     * @return int
     */
    public Integer baseInsertSelective(WmsFailedLibrary entity) {
        return dao.baseInsertSelective(entity);
    }

    /**
     * 删除（根据主键ID删除）
     *
     * @param id 主键
     * @return int
     */
    public Integer baseDeleteByPrimaryKey(String id) {
        return dao.baseDeleteByPrimaryKey(id);
    }

    /**
     * 删除（根据ID 批量删除）
     *
     * @param idList 主键ID列表
     * @return int
     */
    public Integer baseDeleteBathPrimaryKeys(List<String> idList) {
        return dao.baseDeleteBathPrimaryKeys(idList);
    }


    /**
     * 查询（根据主键ID查询）
     *
     * @param id 主键ID
     * @return WmsFailedLibrary
     */
    public WmsFailedLibrary baseSelectByPrimaryKey(String id) {
        return dao.baseSelectByPrimaryKey(id);
    }

    /**
     * 查询（根据ID 批量查询）
     *
     * @param idList 主键ID列表
     * @return list
     */
    public Integer baseSelectBathPrimaryKeys(List<WmsFailedLibrary> idList) {
        return dao.baseSelectBathPrimaryKeys(idList);
    }

    /**
     * 修改 （匹配有值的字段）
     *
     * @param entity 主键ID列表
     * @return int
     */
    public Integer baseUpdateByPrimaryKeySelective(WmsFailedLibrary entity) {
        return dao.baseUpdateByPrimaryKeySelective(entity);
    }

    /**
     * 修改（根据主键ID修改）
     *
     * @param entity 主键ID列表
     * @return int
     */
    public Integer baseUpdateByPrimaryKey(WmsFailedLibrary entity) {
        return dao.baseUpdateByPrimaryKey(entity);
    }
}
